package com.scu.hotelsystem.controller;

import com.scu.hotelsystem.pojo.RoomBusiness;
import com.scu.hotelsystem.service.RoomBusinessService;
import com.scu.hotelsystem.util.DateConverter;
import com.scu.hotelsystem.util.PageInfo;
import com.scu.hotelsystem.util.Result;
import com.scu.hotelsystem.util.RoomOccupyResult;
import com.scu.hotelsystem.util.search.RoomBusinessSearch;
import com.scu.hotelsystem.util.search.RoomOccupySearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
public class RoomBusinessController {
    @Autowired
    private RoomBusinessService roomBusinessService;




    @RequestMapping("/stayBusinessPage")
    public String stayBusinessPage(){
        return "stayBusinessPage";
    }




    @RequestMapping("/searchOccupyRoom")
    public String searchOccupyRoom(){
        return "searchOccupyRoom";
    }

    @RequestMapping("/roomBusinessPage")
    public String reserveToStay(){
        return "roomBusinessPage";
    }

    @RequestMapping("/addStayBusiness")
    public String addStayBusiness(){
        return "staybusiness-add";
    }



    @RequestMapping("/addRoomBusiness")
    public String addRoomBusiness(){
        return "roombusiness-add";
    }

    @RequestMapping("/roomBusinessEditPage")
    public ModelAndView roomBusinessEditPage(int id){
        ModelAndView view=new ModelAndView("roomBusinessEdit");
        RoomBusiness roomBusiness=roomBusinessService.searchRoomBusinessById(id);
        if(roomBusiness!=null){
            view.addObject("roomBusiness",roomBusiness);
        }
        return view;
    }

    @RequestMapping("bookToStay")
    @ResponseBody
    public Result bookToStay(int[] idList){
        if (idList.length == 0){
            return new Result(2, "列表为空");
        }

        int n;
        try{
            n = roomBusinessService.bookToStay(idList);
        }catch (IllegalArgumentException e){
            n = 0;
        }

        if (n == 0){
            return new Result(1, "失败");
        }else {
            return new Result();
        }

    }

    @RequestMapping("/deleteRoomBusiness")
    @ResponseBody
    public Result deleteRoomBusiness(int[] idList){
        if (idList.length == 0){
            return new Result(2, "列表为空");
        }

        int n;
        try{
            n = roomBusinessService.deleteRoomBusiness(idList);
        }catch (IllegalArgumentException e){
            n = 0;
        }

        if (n == 0){
            return new Result(1, "失败");
        }else {
            return new Result();
        }

    }

    @RequestMapping("/updateRoomBusiness")
    @ResponseBody
    public Result updateRoomBusiness(int id, int guest_id_1, int guest_id_2, int room_id, String start_time, String expire_time){
        DateConverter dateConverter=new DateConverter();
        Date start=dateConverter.convert(start_time);
        Date expire=dateConverter.convert(expire_time);
        RoomBusiness roomBusiness=new RoomBusiness(guest_id_1,guest_id_2,1,room_id,start,expire);
        roomBusiness.setId(id);
        int n;
        System.out.println(roomBusiness);
        n=roomBusinessService.updateRoomBusiness(roomBusiness);

        if (n == 1){
            return new Result();
        }else {
            return new Result(1, "未知错误");
        }
    }

    @RequestMapping("/addRoom_Business")
    @ResponseBody
    public Result addRoom_Business(int guest_id_1, int guest_id_2, int room_id, String start_time, String expire_time){
        DateConverter dateConverter=new DateConverter();
        Date start=dateConverter.convert(start_time);
        Date expire=dateConverter.convert(expire_time);
        RoomBusiness roomBusiness=new RoomBusiness(guest_id_1,guest_id_2,1,room_id,start,expire);
        int n;
        System.out.println(roomBusiness);
        n=roomBusinessService.addRoomBusiness(roomBusiness);

        if (n == 1){
            return new Result();
        }else {
            return new Result(1, "未知错误");
        }
    }


    @RequestMapping("/addStay_Business")
    @ResponseBody
    public Result addStay_Business(int guest_id_1, int guest_id_2, int room_id, String start_time, String expire_time){
        DateConverter dateConverter=new DateConverter();
        Date start=dateConverter.convert(start_time);
        Date expire=dateConverter.convert(expire_time);
        RoomBusiness roomBusiness=new RoomBusiness(guest_id_1,guest_id_2,2,room_id,start,expire);
        int n;
        System.out.println(roomBusiness);
        n=roomBusinessService.addRoomBusiness(roomBusiness);

        if (n == 1){
            return new Result();
        }else {
            return new Result(1, "未知错误");
        }
    }

    @RequestMapping("/searchRoomOccupyList")
    @ResponseBody
    public Result searchRoomOccupyList(RoomOccupySearch roomOccupySearch, PageInfo pageInfo){

       List<RoomOccupyResult> roomOccupyResultList;
       int count;
       roomOccupyResultList=roomBusinessService.searchRoomOccupyList(roomOccupySearch,pageInfo.index(), pageInfo.limit());

        count = roomOccupyResultList.size();
        System.out.println(count);
        return new Result(count, roomOccupyResultList);
    }



    @RequestMapping("/searchStayBusinessList")
    @ResponseBody
    public Result searchStayBusinessList(RoomBusinessSearch roomBusinessSearch, PageInfo pageInfo){
        List<RoomBusiness> roomBusinessList;
        int cid=2;
        int count;
        roomBusinessList= roomBusinessService.searchRoomBusinessList(roomBusinessSearch,cid, pageInfo.index(), pageInfo.limit());
        count = roomBusinessList.size();
        System.out.println(count);
        return new Result(count, roomBusinessList);
    }


    @RequestMapping("/searchRoomBusinessList")
    @ResponseBody
    public Result searchRoomBusinessList(RoomBusinessSearch roomBusinessSearch, PageInfo pageInfo){
        List<RoomBusiness> roomBusinessList;

        int cid=1;
        int count;
        System.out.println(roomBusinessSearch.getGuest_id());
        System.out.println(roomBusinessSearch.getRoom_id());
        roomBusinessList= roomBusinessService.searchRoomBusinessList(roomBusinessSearch,cid, pageInfo.index(), pageInfo.limit());
        count = roomBusinessList.size();
        System.out.println(count);
        return new Result(count, roomBusinessList);
    }

}
